package com.mask.im.server.netty.action.handler;

import com.mask.im.server.netty.enums.IMActionEnum;
import com.mask.im.server.netty.model.IMReceiveMessage;
import io.netty.channel.Channel;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @className： MaskGroupChatSocketHandler
 * @Description: 群聊操作
 * @Author： jian
 * @Date： 2025/8/29 11:10
 */
@Component
public class IMGroupChatHandler extends IMAbstractChatHandler {


    @Override
    public IMActionEnum getAction() {
        return IMActionEnum.GROUP_CHAT;
    }

    @Override
    @Transactional
    public void handle(IMReceiveMessage imReceiveMessage, Channel channel) {
//        Long groupChatId = imReceiveMessage.getGroupChatId();
//        String content = (String) imReceiveMessage.getContent();
//        Integer extend = imReceiveMessage.getExtend();
//        Long userInfoId = MaskSecurityUtil.getCurrentUserId();
//
//
//        //判断群聊是否使用中
////        IMGroupChatDO selectById = groupChatMapper.selectById(groupChatId);
//        IMGroupChatDO selectById = new IMGroupChatDO();
//        boolean flag = true;
//        if (selectById.getStatus().equals(CommonConstant.ONE)) {
//            flag = false;
//        }
//
//        //保存聊天记录
//        IMGroupChatMessageDO chatStorage = IMGroupChatMessageDO.builder()
//                .content(content)
//                .groupId(groupChatId)
//                .isDeleted(0)
//                .messageType(0)
//                .sendStatus(flag ? 1 : 0)
//                .systemPrompt(flag ? "" : "群聊已被解散，消息发送失败")
//                .senderId(userInfoId)
//                .messageType(extend).build();
////        groupChatStorageMapper.insert(chatStorage);
//
//        //刷新群聊最新消息
//        IMGroupChatDO imGroupChat = IMGroupChatDO.builder()
//                .recentTime(new Date())
//                .id(groupChatId)
//                .recentMessage(content).build();
////        groupChatMapper.updateById(imGroupChat);
//        //获取插入的消息
//        IMSendMessage websocketDTO = new IMSendMessage();
//        websocketDTO.setCmd(WebSocketConstant.GROUP_CHAT);
//        //得到需要要的推送的一条记录
//        GroupChatMessageDTO groupChatStorageDTO = getGroupChatStorageDTO(chatStorage);
//
//        websocketDTO.setMessage(groupChatStorageDTO);
//        //获取当前加入该群聊的在线人的id
//        List<IMGroupChatUserDO> imGroupChatUsers  = new ArrayList<>();
////        List<IMGroupChatUserDO> imGroupChatUsers = groupChatUserMapper.selectList(new LambdaQueryWrapper<IMGroupChatUserDO>()
////                .select(IMGroupChatUserDO::getUserId, IMGroupChatUserDO::getId, IMGroupChatUserDO::getUnreadCount)
////                .eq(IMGroupChatUserDO::getGroupId, groupChatId));
//        groupChatStorageDTO.setIsMyselfMessage(true);
//        //发送给自己
//        channel.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(websocketDTO, SerializerFeature.WriteMapNullValue)));
//        //发送给群里其他人
//        groupChatStorageDTO.setIsMyselfMessage(false);
//        websocketDTO.setMessage(groupChatStorageDTO);
//        imGroupChatUsers.stream().filter(item -> !item.getUserId().equals(userInfoId))
//                .forEach(groupChatUser -> {
//                    Long infoId = groupChatUser.getUserId();
//                    Channel serviceUserIdChannel = channelService.getUserIdChannel(infoId.toString());
//                    if (serviceUserIdChannel != null) {
//                        serviceUserIdChannel.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(websocketDTO, SerializerFeature.WriteMapNullValue)));
//                    } else {
//                        IMGroupChatUserDO newIMGroupChatUser = new IMGroupChatUserDO();
//                        newIMGroupChatUser.setId(groupChatUser.getId());
//                        newIMGroupChatUser.setUnreadCount(groupChatUser.getUnreadCount() + 1);
////                        groupChatUserMapper.updateById(newIMGroupChatUser);
//                    }
//                });
    }

//    private GroupChatMessageDTO getGroupChatStorageDTO(IMGroupChatMessageDO chatStorage) {
//        IMGroupChatUserDO imGroupChatUser = new IMGroupChatUserDO();
////        IMGroupChatUserDO imGroupChatUser = groupChatUserMapper.selectOne(new LambdaQueryWrapper<IMGroupChatUserDO>()
////                .eq(IMGroupChatUserDO::getGroupId, chatStorage.getGroupId())
////                .eq(IMGroupChatUserDO::getUserId, chatStorage.getSenderId()));
////        UserInfo userInfo = userInfoMapper.selectById(chatStorage.getSenderId());
//        UserInfo userInfo = new UserInfo();
//        GroupChatMessageDTO groupChatStorageDTO = new GroupChatMessageDTO();
//        groupChatStorageDTO.setGroupChatStorageId(chatStorage.getId());
//        groupChatStorageDTO.setGroupChatId(chatStorage.getGroupId());
//        groupChatStorageDTO.setMemberId(chatStorage.getSenderId());
//        groupChatStorageDTO.setIsRetracted(chatStorage.getIsDeleted());
//        groupChatStorageDTO.setContent(chatStorage.getContent());
//        groupChatStorageDTO.setMessageType(chatStorage.getMessageType());
//        groupChatStorageDTO.setIsSendSuccessfully(chatStorage.getSendStatus());
//        groupChatStorageDTO.setMemberAvatar(userInfo.getAvatar());
//        groupChatStorageDTO.setUsername(userInfo.getUsername());
//        groupChatStorageDTO.setCustomizeRemark(imGroupChatUser.getCustomNickname());
//        groupChatStorageDTO.setDeletedBy(chatStorage.getDeletedUserIds());
//        groupChatStorageDTO.setCreateTime(chatStorage.getCreateTime());
//        groupChatStorageDTO.setIsRetracted(chatStorage.getIsDeleted());
//        return groupChatStorageDTO;
//    }
}
